package com.example.class_manage.Config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;


@Configuration
public class DruidConfig {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DruidDataSource druidSource()
    {
        return new DruidDataSource();
    }

    @Bean
    //后台监控
    public ServletRegistrationBean statViewServlet()
    {
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
        //后台登录密码账号配置
        HashMap<String,String> init = new HashMap<>();
        init.put("loginUsername","admin");//账号
        init.put("loginPassword","123456");//密码
        //允许访问
        init.put("allow","");//后面的空格可以加ip  禁止是“deny”
        //也可以设计禁止
        init.put("resetEnable","false");  // 是否能够重置数据
        bean.setInitParameters(init);
        return bean;
    }
}
